package com.example.fw;

import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class ApplicationManager {

	private  WebDriver driver;
	public  String baseUrl;

	private GroupHelper groupHelper;
	private NavigationHelper navigationHelper;
	private ContactHelper contactHelper;
	private Properties properties;
	private HibernateHelper hibernateHelper;
	
	public ApplicationManager(Properties properties){
		this.properties = properties;
	}
	
	public void returnToMainPage() { driver.findElement(By.linkText("home page")).click(); }

	public void stop() { driver.quit(); }
		
	public NavigationHelper navigateTo(){
	if (navigationHelper == null)
	{ navigationHelper = new NavigationHelper(this);}
	return navigationHelper; 
	}
	
	public GroupHelper getGroupHelper(){
	if (groupHelper == null)
	{ groupHelper = new GroupHelper(this);}
	return groupHelper; 
	}
	
	public ContactHelper getContactHelper(){
	if (contactHelper == null)
	{contactHelper = new ContactHelper(this);}
	return contactHelper; 
	}

	public WebDriver getDriver() {
		if (driver == null){
			String browser = properties.getProperty("browser");
			if ("firefox".equals(browser))
			{
			driver = new FirefoxDriver();
			}
			else if ("ie".equals(browser))
			{
			driver = new InternetExplorerDriver();
			}
			else
			{throw new Error ("Unsupported browser " + browser);}
			System.out.println(browser);
			baseUrl = properties.getProperty("baseUrl");
			//baseUrl = "http://localhost/addressbookv4.1.4/";
			System.out.println(baseUrl);
			//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			driver.get(baseUrl);
		
		}
		return driver; 
	}

	public HibernateHelper getHibernateHelper() {
		if (hibernateHelper == null){
			hibernateHelper = new HibernateHelper(this);
			}
		return hibernateHelper; 	
	}
	
}